package com.qiandw.config;

import com.qiandw.security.config.WebSecurityConfigImpl;
import com.qiandw.security.handle.LoginSuccessHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author coxx
 */

@EnableWebSecurity
public class AuthConfig extends WebSecurityConfigImpl {

    @Autowired
    public AuthConfig(@Qualifier("userDetailServiceImpl") UserDetailsService userDetailsService) {
        super(userDetailsService);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests()
                .antMatchers("/login", "/error").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .successHandler(new LoginSuccessHandle())
                //.failureForwardUrl("/error")
                .and()
                .logout().logoutUrl("/logout")
                .and()
                .csrf().disable()
                .sessionManagement()
                .maximumSessions(2);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/webjars/**", "/static/**");
    }

}
